
package br.com.rafael.gestorinvestimentos.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.*;
import java.text.DecimalFormat;
import java.util.Locale;

import junit.framework.TestCase;
import br.com.rafael.gestorinvestimentos.model.Cotacao;
import br.com.rafael.gestorinvestimentos.util.Statistics;

public class StatisticsTest extends TestCase {
	
	public double[] setValues(String path){
		String fileFolder = path;
		String s;
		int i=0;
		double[] values = new double[19];
		
		File file = new File(fileFolder);
		try {
			FileReader fr = new FileReader(file);
			BufferedReader buffer = new BufferedReader(fr);
			
			while((s=buffer.readLine())!=null){             
			    values[i]=(Double.parseDouble(s.replace(',','.').substring(11)));
			    i++;                               
			}
			 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return values;
	}
	
	public void testArithmeticAverage() {
		Locale.setDefault(Locale.US);
		DecimalFormat dec = new DecimalFormat("0.00000000");

		assertTrue(Double.parseDouble(dec.format(Statistics.arithmeticAverage(this.setValues("C:\\development\\workspace\\GestorInvestimentos\\retornoPETR.txt"))))==31.96700518);
	}

	public void testStandardDeviation() {
		Locale.setDefault(Locale.US);
		DecimalFormat dec = new DecimalFormat("0.00000000");
		assertTrue(Double.parseDouble(dec.format(Statistics.standardDeviation(this.setValues("C:\\development\\workspace\\GestorInvestimentos\\retornoPETR.txt"))))==12.25089780);
	}

	public void testVariance() {
		Locale.setDefault(Locale.US);
		DecimalFormat dec = new DecimalFormat("0.00000000");
	
		assertTrue(Double.parseDouble(dec.format(Statistics.variance(this.setValues("C:\\development\\workspace\\GestorInvestimentos\\retornoPETR.txt"))))==150.08449691);
	}

	public void testCovariance() {
		Locale.setDefault(Locale.US);
		DecimalFormat dec = new DecimalFormat("0.00000000");
	
		double [] data1=this.setValues("C:\\development\\workspace\\GestorInvestimentos\\retornoPETR.txt");
		double [] data2=this.setValues("C:\\development\\workspace\\GestorInvestimentos\\retornoBNCA.txt");
		try {
			assertTrue(Double.parseDouble(dec.format(Statistics.covariance(data1,data2)))==4.87475735);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void testCorrelation() {
		Locale.setDefault(Locale.US);
		DecimalFormat dec = new DecimalFormat("0.00000000");
	
		double [] data1=this.setValues("C:\\development\\workspace\\GestorInvestimentos\\retornoPETR.txt");
		double [] data2=this.setValues("C:\\development\\workspace\\GestorInvestimentos\\retornoBNCA.txt");
		try {
			assertTrue(Double.parseDouble(dec.format(Statistics.correlation(data1,data2)))==0.95793481);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void testPercentageReturn() {
		double [] returns = Statistics.percentageReturn(this.setValues("C:\\development\\workspace\\GestorInvestimentos\\retornoPETR.txt"));
		for(int i=0; i<returns.length;i++){
			System.out.println(returns[i]);
		}
	}

	public void testLogReturn() {
		double [] returns = Statistics.logReturn(this.setValues("C:\\development\\workspace\\GestorInvestimentos\\retornoPETR.txt"));
		for(int i=0; i<returns.length;i++){
			System.out.println(returns[i]);
		}

	}

}